<?php
/**
 */
// from 0 local 1 from_mblog

define("CACHE_PREFIX_PUBU","_pubu_content");

class db_mblog_text_content extends db_base {
    
	var $table_name = "mblog_text_content";
	
	function db_mblog_text_content()
	{
		parent::db_base();
	}
	
	

	function get_mblog_text_content_item($id)
	{
		$mc = mem_cache::get_instance();
	
		$key = $this->table_name."-get_mblog_text_content_item-{$id}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$id = intval($id);
		
			$where_ary = array();
			$where_ary[] = "id = $id";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,1,1,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	
	function get_mblog_text_content_list_by_topic_id($p,$pcount,$topic_id,$create_time,$tag_id,$next_time)
	{
		$mc = mem_cache::get_instance();
	
		$key = $this->table_name."-get_mblog_text_content_list_by_topic_id-{$p}-{$pcount}-{$topic_id}-{$create_time}-{$tag_id}-{$next_time}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			
			$p = intval($p);
			$pcount = intval($pcount);
			$topic_id = intval($topic_id);
			$tag_id = intval($tag_id);
			$create_time = mysql_escape_string($create_time);
			
			$where_ary = array();
			$where_ary[] = "topic_id = $topic_id";
			$where_ary[] = "tag_id = $tag_id";
			if($create_time)$where_ary[] = "create_time >= '{$create_time}'";
			if($next_time) $where_ary[] = "create_time <= '{$next_time}'";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,$p,$pcount,"create_time",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	
	
	
  	function get_user_order_by_count_tuijian_nocache($show_type,$p,$pcount)
	{
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$topic_list = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_like($show_type,1,10);
		
		
		$content = array();
		foreach($topic_list as $value)
		{
			$topic_id = $value["id"];
			$topic_id = intval($topic_id);
			$sql ="select * from mblog_text_content where topic_id = $topic_id order by  update_date desc , count_tuijian desc limit 1";
			
			$res = $db_mblog_cron_topic_tag->select($sql);
			
			$res = $res[0];
			$content[] = $res;
		}
		
		return $content ;
	}
	
###########################

		//抓取用户最近的微博
	function cron_get_mblog_count_search_content($content)
	{
		$where = "content_index  like '{$content}%' ";
		
		$count = $this->get_listcount($this->table_name,$where)	;
		
		return $count;
	}
	
	
	function cron_insert_mblog_text_content($content,$topic_id,$topic,$source,$from,
	$uid,$nickname,$update_date,$mid,$create_time,$tag_id,$tag_name,$head_url)
	{
		if(!$mid)
		{
			return false;
		}
		//内容先转码
		$content = html_entity_decode($content);
		//删除@信息
		$check_content= preg_replace("/@([\x{4e00}-\x{9fa5}0-9A-Za-z_\-]+)/u","",$content);
		echo "check_content : $check_content \n";
		
		if($topic)
		{
			if(stripos($check_content,$topic)=== false)//删除@信息后检查话题是否在内容里，如果不在返回错误
			{
				$check_content_at = preg_match_all("/@([\x{4e00}-\x{9fa5}0-9A-Za-z_\-]+)/u",$content,$match);
				
				if($match[1])
				{
					
					foreach($match[1] as $value) //如果在外部内容找不到这个话题，并且在用户名里找到这个话题，返回false
					{
						if(stripos($value,$topic) !== false)
						{
							echo "false \n";
							return false;
						}
					
					}
				
				}
				
				
				
			}
				
		}
		
		//删除除了汉字和字母数字外的其他字符
		$check_content = preg_replace("/#.*?#/","",$check_content);
		$check_content= preg_replace("/[^\x{4e00}-\x{9fa5}]+/u","",$check_content);
		if(strlen($check_content) > 30)//如果是15个中文字符长度以上，就需要判断内容做key
		{
			
			//如果数据库里已经有这样的数据，不入库
			$find_content = string_utf8::substr_utf8_cn_v2($check_content,60);
			$get_res_count = $this->cron_get_mblog_count_search_content($find_content);
			if($get_res_count > 0)
			{
				return false;
			}
			
			
			$md5_key = $check_content;//内容保持唯一
			$md5_key =  md5($md5_key);
			
			
		}
		else
		{
			$find_content = $check_content;
			$md5_key = $mid;
			$md5_key =  md5($md5_key);
		}
		
		
		
		$ary["content_index"] = $find_content;
		$ary["tag_id"] = $tag_id;
		$ary["tag_name"] = $tag_name;
		$ary["head_url"] = $head_url;
	
		$ary["content"] = $content;

		$ary["topic_id"] = $topic_id;
		$ary["topic"] = $topic;
		$ary["source"] = $source;
		$ary["show_type"] = $show_type;
		$ary["from"] = $from;
		$ary["mid"] = $mid;
		
		
		
		$ary["status"] = 1;
		$ary["create_uid"] = $uid;
		$ary["create_nickname"] = $nickname;
		$ary["create_time"] = $create_time;
		$ary["create_ip"] = 0;
		
		$update_date = strtotime($create_time);
		
		$ary["update_date"] = $update_date;
		
		
		$ret = $this->insert($this->table_name, $ary );
		
		return $this->get_lastinsertid();
		
	}
	
	
	
	/**
	 * cron 获取food_tag
	 */
	function cron_get_text_count($start_id)
	{
		$start_id = intval($start_id);
		$where=" id > $start_id  and  status = 1 " ;
		$data = $this->get_listcount($this->table_name, $where);
		
		return $data;
	}
	
		
	function cron_get_text_list($p,$pcount,$start_id)
	{
		$start_id = intval($start_id);
		$where=" id > $start_id  and  status = 1 " ;
		$data = $this->get_alllist($this->table_name,$p,$pcount,"id", $where);
		
		return $data;
	}
	
	
	
	
}



